package com.sias.realestate.web.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.sias.realestate.web.model.Owner;
import com.sias.realestate.web.model.Tenement;
import com.sias.realestate.web.model.User;
import com.sias.realestate.web.service.OwnerService;
import com.sias.realestate.web.service.TenementService;
import com.sias.realestate.web.service.UserService;
import com.sias.realestate.web.util.FinalVarUtil;
import com.sias.realstate.web.pagemodel.PageModel;

@Controller
@RequestMapping(value = "/")
public class UserController {

	@Resource
	private UserService userService;
	@Resource
	private OwnerService ownerService;
	@Resource
	private TenementService tenementService;

	@RequestMapping(value = "/getAllUser", method = RequestMethod.GET)
	public ModelAndView getAllUser(HttpServletRequest request)
			throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		ModelAndView mv = new ModelAndView();
		PageModel pageModel = new PageModel();
		List<User> list = userService.selectList();
		String th_list = FinalVarUtil.UserTh;
		pageModel.setTh_list(th_list);
		mv.addObject("pageModel", pageModel);
		mv.addObject("list", list);
		mv.setViewName("user/userList");
		return mv;
	}

	/**
	 * 
	 * @param model
	 * @param request
	 * @return 方法说明：跳转到新建用户的界面 创建日期：2016年12月13日下午5:33:28
	 */
	@RequestMapping(value = "/addUserJsp", method = RequestMethod.GET)
	public ModelAndView addUserJsp(HttpServletRequest request)
			throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("user/addUser");
		return mv;
	}

	/**
	 * 
	 * @param model
	 * @param request
	 * @return 方法说明：添加新用户功能 创建日期：2016年12月13日下午5:34:48
	 */
	@RequestMapping(value = "/addUser", method = RequestMethod.POST)
	public ModelAndView addUser(HttpServletRequest request, User user) {
		ModelAndView mv = new ModelAndView();
		if (userService.findByUname(user.getuName()) == null) {
			if(ownerService.findByIdcard(user.getuIdcard())==null){
				user.setuStatus("2");
				userService.insert(user);
				request.setAttribute("successInfo", "注册成功！请您登录");
				mv.setViewName("/success-info");
				mv.setViewName("../../login");
			}else{
				user.setuStatus("1");
				userService.insert(user);
				request.setAttribute("successInfo", "注册成功！请您登录");
				mv.setViewName("/success-info");
				mv.setViewName("../../login");
			}
		}else{
			request.setAttribute("failedInfo", "注册失败！用户名重复");
			mv.setViewName("/failed-info");
			mv.setViewName("user/addUser");
		}
		return mv;
	}

	/**
	 * 
	 * @param model
	 * @param request
	 * @return 方法说明：跳转到修改用户的界面 创建日期：2016年12月13日下午5:33:28
	 */
	@RequestMapping(value = "/editUserJsp", method = RequestMethod.GET)
	public ModelAndView editUserJsp(HttpServletRequest request, Integer uId)
			throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		ModelAndView mv = new ModelAndView();
		User user = userService.selectById(uId);
		mv.addObject("user", user);
		mv.setViewName("user/editUser");
		return mv;
	}

	/**
	 * 
	 * @param model
	 * @param request
	 * @return 方法说明：修改用户信息 创建日期：2016年12月13日下午5:35:27
	 */
	@RequestMapping(value = "/editUser", method = RequestMethod.POST)
	public ModelAndView editUser(HttpServletRequest request, User user) {
		ModelAndView mv = new ModelAndView();
		userService.update(user);
		user = userService.selectById(user.getuId());
		Owner owner = ownerService.findByIdcard(user.getuIdcard());// 待传参数
		Tenement tenement = tenementService.findTenementByIdcard(owner.getoIdcard());
		mv.addObject("owner", owner);
		mv.addObject("user", user);
		mv.addObject("tenement", tenement);

		mv.setViewName("owner/ownerInfo");
		return mv;
	}

	/**
	 * 
	 * @param model
	 * @param request
	 * @return 方法说明：跳转到修改密码的界面 创建日期：2016年12月13日下午5:33:28
	 */
	@RequestMapping(value = "/editPasswordJsp", method = RequestMethod.GET)
	public ModelAndView editPasswordJsp(HttpServletRequest request, Integer uId) {
		ModelAndView mv = new ModelAndView();
		User user = userService.selectById(uId);
		mv.addObject("user", user);
		mv.setViewName("user/editPassword");
		return mv;
	}

	/**
	 * 
	 * @param model
	 * @param request
	 * @return 方法说明：修改密码 创建日期：2016年12月13日下午5:33:28
	 */
	@RequestMapping(value = "/editPassword", method = RequestMethod.POST)
	public ModelAndView editPassword(HttpServletRequest request, User user) {
		ModelAndView mv = new ModelAndView();
		userService.update(user);
		request.getSession().removeAttribute("user");
		mv.setViewName("../../login");
		return mv;
	}

	/**
	 * 
	 * @param model
	 * @param request
	 * @return 方法说明：注销该用户信息 创建日期：2016年12月13日下午5:35:27
	 */
	@RequestMapping(value = "/cancelUser", method = RequestMethod.GET)
	public ModelAndView cancelUser(HttpServletRequest request, Integer uid) {
		ModelAndView mv = new ModelAndView("redirect:getAllUser");
		User user = userService.selectById(uid);
		Owner owner = ownerService.findByIdcard(user.getuIdcard());
		if(owner!=null){
			request.setAttribute("failedInfo", "该用户在住，不可删除！");
			mv.setViewName("/failed-info");
		}else{
			user.setuStatus("-1");// 注销用户
			userService.update(user);
		}
		return mv;
	}
	/**
	 * 
	 * @param model
	 * @param request
	 * @return 方法说明：删除该用户信息 创建日期：2016年12月13日下午5:35:27
	 */
	@RequestMapping(value = "/deleteUser", method = RequestMethod.GET)
	public ModelAndView deleteUser(HttpServletRequest request, Integer uid) {
		ModelAndView mv = new ModelAndView("redirect:getAllUser");
		userService.delete(uid);
		return mv;
	}

	/**
	 * 
	 * @param model
	 * @param request
	 * @return 方法说明：登录 创建日期：2016年12月20日下午2:39:45
	 */
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public ModelAndView login(HttpServletRequest request, User user) {
		ModelAndView mv = new ModelAndView();
		User confirm = userService.findByUname(user.getuUname());
		if (confirm != null) {
			if (confirm.getuPassword().equals(user.getuPassword())) {
				request.getSession().setAttribute("user", confirm);
				if (confirm.getuStatus().equals("0")) {
					mv.setViewName("/index22");
				}
				if (confirm.getuStatus().equals("1")) {
					mv.setViewName("/index");
				}
				if (confirm.getuStatus().equals("2")) {
					mv.setViewName("/index");
				}
			} else {
				request.setAttribute("failedInfo", "登录失败,密码错误！");
				mv.setViewName("../../login");
			}
		} else {
			request.setAttribute("failedInfo", "登录失败,没有该用户！");
			mv.setViewName("../../login");
		}
		return mv;
	}

	/**
	 * 
	 * @param model
	 * @param request
	 * @return 方法说明：登出 创建日期：2016年12月21日上午11:25:39
	 */
	@RequestMapping(value = "/logout", method = RequestMethod.GET)
	public ModelAndView logout(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();
		request.getSession().removeAttribute("user");
		mv.setViewName("../../login");
		return mv;
	}
}
